package com.gy.dalian.controller.getData;

import com.gy.dalian.service.DldpResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/dw-service-xxl-screen/DldpResultXinController")
@RequiredArgsConstructor
@Component
public class DldpResultXinController {

    private final DldpResultService dldpResultService;
    @Value("${serviceUrl}")
    private String serviceUrl;

    @PostMapping("getResult")
    @ResponseBody
    public Map<String, Object> result() {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> resultMap2 = new HashMap<>();
        dldlLeftTop1(resultMap2);
        dldlLeftTop2(resultMap2);
        dldlLeftTop3(resultMap2);
        dldlLeftTop4(resultMap2);
        dldlRightTop1(resultMap2);
        resultMap.put("p1_2", resultMap2);

        ywjxOneLeftTop1(resultMap2);
        ywjxOneLeftTop2(resultMap2);
        ywjxOneMiddleTop1(resultMap2);
        ywjxOneMiddleTop2(resultMap2);
        ywjxOneRightTop1(resultMap2);
        ywjxOneRightTop2(resultMap2);
        resultMap.put("p2_2", resultMap2);

        ywjxTwoLeftTop1(resultMap2);
        ywjxTwoLeftTop2(resultMap2);
        ywjxTwoRightTop1(resultMap2);
        ywjxTwoRightTop2(resultMap2);
        resultMap.put("p2_1", resultMap2);

        ywjxThreeLeftTop1(resultMap2);
        ywjxThreeLeftTop2(resultMap2);
        ywjxThreeRightTop1(resultMap2);
        ywjxThreeRightTop2(resultMap2);
        ywjxThreeRightBottom1(resultMap2);
        resultMap.put("p2_3", resultMap2);
        return resultMap;
    }

    private void dldlLeftTop1(Map<String, Object> resultMap) {
        String sql = "select zb as value from  dldl_nylxfbqk  where zbqs  = (select max(zbqs) from dldl_nylxfbqk) and type  = 1\n" + "UNION ALL\n" + "select SUM(fdl) as value from  dldl_qjnyfdqk  where zbqs  = (select max(zbqs) from dldl_qjnyfdqk) \n" + "UNION ALL\n" + "select  SUM(zs) as value from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb) and gf_type = 0\n" + "UNION ALL\n" + "select SUM(zs) as value from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb) and gf_type = 1";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        List<Map<String, Object>> leftTop1 = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("label", "大连地区清洁能源（包含核电）发电量（周）");
        map1.put("value", result.get(1).get("value"));
        map1.put("unit1", "亿");
        map1.put("unit2", "千瓦时");
        leftTop1.add(map1);

        map1 = new HashMap<>();
        map1.put("label", "占地区总发电量比例（年）");
        map1.put("value", result.get(0).get("value"));
        map1.put("unit1", "");
        map1.put("unit2", "%");
        leftTop1.add(map1);

        map1 = new HashMap<>();
        map1.put("label", "大连地区接入分布式光伏（年）");
        map1.put("value", result.get(2).get("value"));
        map1.put("unit1", "");
        map1.put("unit2", "户");
        leftTop1.add(map1);

        map1 = new HashMap<>();
        map1.put("label", "并网容量（年）");
        map1.put("value", result.get(3).get("value"));
        map1.put("unit1", "");
        map1.put("unit2", "千伏安");
        leftTop1.add(map1);

        resultMap.put("left_top_1", leftTop1);
    }

    private void dldlLeftTop2(Map<String, Object> resultMap) {
        String sql = "SELECT nylx as name,zb as value FROM dldl_nylxfbqk  WHERE zbqs = ( SELECT MAX( zbqs ) FROM dldl_nylxfbqk ) and type = 0 ";
        Map<String, Object> map = new HashMap<>();
        map.put("title", "大连发电量能源类型分布情况");
        map.put("data", queryInfoBySql(sql));
        resultMap.put("left_top_2", map);
    }

    private void dldlLeftTop3(Map<String, Object> resultMap) {
        String sql = "SELECT * FROM dldl_qjnyfdqk  WHERE zbqs = ( SELECT MAX( zbqs ) FROM dldl_qjnyfdqk ) ORDER BY px";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        List<Map<String, Object>> leftTop3 = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("label", stringObjectMap.get("nylx"));
            map1.put("value", stringObjectMap.get("zs"));
            map1.put("unit", "座");
            map1.put("value1", stringObjectMap.get("fdl"));
            map1.put("unit1", "万千瓦时");
            leftTop3.add(map1);
        }
        resultMap.put("left_top_3", leftTop3);
    }

    private void dldlLeftTop4(Map<String, Object> resultMap) {
        String sql = "SELECT a.* FROM (select  * from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb)) a LEFT JOIN dldp_jg b ON a.dw = b.jc WHERE gf_type = 0 AND data_type = 0 ORDER BY b.px + 0";
        List<Map<String, Object>> result1 = dldlLeftTop4GetData(dldpResultService.queryInfoBySql(sql));

        sql = "SELECT a.* FROM (select  * from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb)) a LEFT JOIN dldp_jg b ON a.dw = b.jc WHERE gf_type = 0 AND data_type = 1 ORDER BY b.px + 0";
        List<Map<String, Object>> result2 = dldlLeftTop4GetData(dldpResultService.queryInfoBySql(sql));

        sql = "SELECT a.* FROM (select  * from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb)) a LEFT JOIN dldp_jg b ON a.dw = b.jc WHERE gf_type = 1 AND data_type = 0 ORDER BY b.px + 0";
        List<Map<String, Object>> result3 = dldlLeftTop4GetData(dldpResultService.queryInfoBySql(sql));

        sql = "SELECT a.* FROM (select  * from dldl_gdwfbsgffb where zbqs  = (select max(zbqs) from dldl_gdwfbsgffb)) a LEFT JOIN dldp_jg b ON a.dw = b.jc WHERE gf_type = 1 AND data_type = 1 ORDER BY b.px + 0";
        List<Map<String, Object>> result4 = dldlLeftTop4GetData(dldpResultService.queryInfoBySql(sql));

        Map<String, Object> map1 = new HashMap<>();
        map1.put("data", new Object[]{result1, result2});
        map1.put("label", "户数");
        map1.put("legend", new String[]{"自然人光伏户数（户）", "非自然人光伏户数（户）"});

        Map<String, Object> map2 = new HashMap<>();
        map2.put("data", new Object[]{result3, result4});
        map2.put("label", "并网容量");
        map2.put("legend", new String[]{"并网容量1", "并网容量2"});

        Map<String, Object> map3 = new HashMap<>();
        map3.put("data", new Object[]{map1, map2});
        map3.put("title", "各单位分布式光伏分布情况");
        resultMap.put("left_top_4", map3);
    }

    private void dldlRightTop1(Map<String, Object> resultMap) {
        String sql = "SELECT dzsm label,dzsl AS 'value','座' unit,dzjtsm label1,dzjtms value1,CONCAT(' " + serviceUrl + "'" + ", img_url ) img " + " FROM dldl_cndzxx WHERE zbqs = ( SELECT MAX( zbqs )  FROM dldl_cndzxx )";
        resultMap.put("right_top_1", queryInfoBySql(sql));
    }

    private List<Map<String, Object>> dldlLeftTop4GetData(List<Map<String, Object>> result) {
        List<Map<String, Object>> resultMap = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", stringObjectMap.get("dw"));
            map1.put("value", stringObjectMap.get("zs"));
            resultMap.add(map1);
        }
        return resultMap;
    }

    private void ywjxOneLeftTop1(Map<String, Object> resultMap) {
        String sql = "select IFNULL(zyl,0)zyl,IFNULL(szyl,0)szyl,IFNULL(zywz,0)zywz  from ywjx_xczy where nyz = (select MAX(nyz) from ywjx_xczy where t_type=0 ) and t_type = 0 ";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> d1 = new HashMap<>();
        d1.put("label", "执行现场作业（周）");
        d1.put("value", result.get(0).get("zyl"));
        d1.put("unit", "项次");

        Map<String, Object> d2 = new LinkedHashMap<>();
        d2.put("label", "三级及以上作业风险（周）");
        d2.put("value", result.get(0).get("szyl"));
        d2.put("unit", "项次");

        Map<String, Object> d3 = new LinkedHashMap<>();
        d3.put("label", "带电作业（周）");
        d3.put("value", result.get(0).get("zywz"));
        d3.put("unit", "次");

        List<Map<String, Object>> leftTop1 = new ArrayList<>();
        leftTop1.add(d1);
        leftTop1.add(d2);
        leftTop1.add(d3);
        resultMap.put("left_top_1", leftTop1);
    }

    private void ywjxOneLeftTop2(Map<String, Object> resultMap) {
        String xczym = "select d.*  from (select nyz,zyl from ywjx_xczy where t_type=1 order by nyz desc limit 12) d order by d.nyz";
        List<Map<String, Object>> xczymRes = queryInfoBySql(xczym);
        String xczyw = "select d.*,(select endtime from dldp_qs where zbqs=d.nyz and qstype=1) endtime  from (select nyz,zyl from ywjx_xczy where t_type=0 order by nyz desc limit 10) d order by d.nyz";
        List<Map<String, Object>> xczywRes = queryInfoBySql(xczyw);
        Map<String, Object> mData = new LinkedHashMap<>();
        Map<String, Object> wdata = new LinkedHashMap<>();
        List<Map<String, Object>> mDataList = new ArrayList<>();
        List<Map<String, Object>> wDataList = new ArrayList<>();
        for (Map<String, Object> res : xczymRes) {
            Map<String, Object> m = new LinkedHashMap<>();
            m.put("name", res.get("nyz"));
            m.put("value", res.get("zyl"));
            m.put("unit", "");
            mDataList.add(m);
        }
        for (Map<String, Object> res : xczywRes) {
            Map<String, Object> w = new LinkedHashMap<>();
            w.put("name", res.get("endtime"));
            w.put("value", res.get("zyl"));
            w.put("unit", "");
            wDataList.add(w);
        }
        mData.put("label", "月作业量统计");
        mData.put("legend", new String[]{"月作业量（次）"});
        mData.put("data", new Object[]{mDataList});
        wdata.put("label", "周作业量统计");
        wdata.put("legend", new String[]{"周作业量（项次）"});
        wdata.put("data", new Object[]{wDataList});

        Map<String, Object> leftTop2 = new LinkedHashMap<>();
        List<Map<String, Object>> leftTop2Data = new ArrayList<>();
        leftTop2Data.add(mData);
        leftTop2Data.add(wdata);
        leftTop2.put("title", "大连现场作业趋势变化情况");
        leftTop2.put("data", leftTop2Data);
        resultMap.put("left_top_2", leftTop2);
    }

    private void ywjxOneMiddleTop1(Map<String, Object> resultMap) {
        String sql = "select  wzlx label,wzcs 'value' ,'次' unit  from ywjx_dcwz_info  where zbqs = (select  max(zbqs) from ywjx_dcwz_info) ";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Integer total = result.stream().mapToInt(e -> Integer.parseInt(e.get("value").toString())).sum();
        Map<String, Object> map = new HashMap<>();
        map.put("label", "督查违章");
        map.put("value", total);
        map.put("unit", "次");

        sql = "select  lx 'name',zb 'value'  from ywjx_dcwz_tjt  where zbqs = (select  max(zbqs) from ywjx_dcwz_tjt)";
        map.put("data", new Object[]{queryInfoBySql(sql)});
        resultMap.put("middle_top_1", map);
    }

    private void ywjxOneRightTop1(Map<String, Object> resultMap) {
        String xltd = "select bqhjhb,bqhj from zdgz_tdxl_gdwpdxlgztj  where  zbqs=(select MAX(zbqs) from zdgz_tdxl_gdwpdxlgztj) and xh='-'";
        List<Map<String, Object>> xltdRes = dldpResultService.queryInfoBySql(xltd);
        List<Map<String, Object>> rightTop1 = new ArrayList<>();
        Map<String, Object> xltdData = new LinkedHashMap<>();
        Map<String, Object> xltdhbData = new LinkedHashMap<>();
        xltdData.put("label", "10千伏线路停电（周）");
        xltdData.put("value", isNullValue(xltdRes.get(0), "bqhj"));
        xltdData.put("unit", "条次");
        xltdhbData.put("label", "环比");
        xltdhbData.put("value", isNullValue(xltdRes.get(0), "bqhjhb"));
        xltdhbData.put("unit", "%");
        rightTop1.add(xltdData);
        rightTop1.add(xltdhbData);
        resultMap.put("right_top_1", rightTop1);
    }

    private void ywjxOneMiddleTop2(Map<String, Object> resultMap) {
        String sql = "select  wzlx label,wzcs 'value' ,'次' unit  from ywjx_dcwz_info  where zbqs = (select  max(zbqs) from ywjx_dcwz_info) ";
        resultMap.put("middle_top_2", queryInfoBySql(sql));
    }

    private void ywjxOneRightTop2(Map<String, Object> resultMap) {
        String sql = "select chbl , dw ,chlh , jdgz from zdgz_tdxl_gdwpdxlgztj where dw!='大连' AND zbqs=(select max(zbqs) from zdgz_tdxl_gdwpdxlgztj)\n" + "group by dw order by dw ";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new HashMap<>();
        map.put("label", "配电线路故障（周）");
        map.put("legend", new String[]{"重合不良（次）", "重合良好（次）", "接地故障（次）"});
        List<List<Map<String, Object>>> data = new ArrayList<>();
        List<Map<String, Object>> d1 = new ArrayList<>();
        data.add(d1);
        List<Map<String, Object>> d2 = new ArrayList<>();
        data.add(d2);
        List<Map<String, Object>> d3 = new ArrayList<>();
        data.add(d3);
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("name", isNullValue(stringObjectMap, "dw"));
            m1.put("value", new BigDecimal(isNullValue(stringObjectMap, "chbl").toString()));
            d1.add(m1);
            Map<String, Object> m2 = new HashMap<>();
            m2.put("name", isNullValue(stringObjectMap, "dw"));
            m2.put("value", new BigDecimal(isNullValue(stringObjectMap, "chlh").toString()));
            d2.add(m2);
            Map<String, Object> m3 = new HashMap<>();
            m3.put("name", isNullValue(stringObjectMap, "dw"));
            m3.put("value", new BigDecimal(isNullValue(stringObjectMap, "jdgz").toString()));
            d3.add(m3);
        }
        map.put("data", data);
        resultMap.put("right_top_2", map);
    }

    private void ywjxTwoLeftTop1(Map<String, Object> resultMap) {
        String sql = "select bxhj,mswhbxl from zdgz_gzbx_bxgt  where dw='大连' AND zbqs=(select max(zbqs) from zdgz_gzbx_bxgt) ";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> leftTop1 = new HashMap<>();
        leftTop1.put("label", "95598抢修工单（周）");
        leftTop1.put("value", isNullValue(result.get(0), "bxhj"));
        leftTop1.put("unit", "件");
        Map<String, Object> leftTop2 = new HashMap<>();
        leftTop2.put("label", "每十万户\n报修数量");
        leftTop2.put("value", isNullValue(result.get(0), "mswhbxl"));

        sql = "select bqddxc,bqxfsc from zdgz_gzbx_gzqxpjclsc  where dw='大连' AND zbqs=(select max(zbqs) from zdgz_gzbx_gzqxpjclsc) ";
        result = queryInfoBySql(sql);
        Map<String, Object> leftTop3 = new HashMap<>();
        leftTop3.put("label", "故障抢修平均到达现场时长");
        leftTop3.put("value", isNullValue(result.get(0), "bqddxc"));
        leftTop3.put("unit", "分钟");
        Map<String, Object> leftTop4 = new HashMap<>();
        leftTop4.put("label", "故障抢修平均修复时长");
        leftTop4.put("value", isNullValue(result.get(0), "bqxfsc"));
        leftTop4.put("unit", "分钟");

        resultMap.put("left_top_1", new Object[]{leftTop1, leftTop2, leftTop3, leftTop4});
    }

    private void ywjxTwoLeftTop2(Map<String, Object> resultMap) {
        String sql = "select gygz ,dygz,fdlgz,tdcx,jlgz,dnzl  from zdgz_gzbx_bxgt  where dw='大连' AND zbqs=(select max(zbqs) from zdgz_gzbx_bxgt)";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new HashMap<>();
        map.put("label", "故障报修类型统计（周）");
        map.put("legend", new String[]{});
        List<List<Map<String, Object>>> data = new ArrayList<>();
        List<Map<String, Object>> d1 = new ArrayList<>();
        data.add(d1);
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> m1 = new HashMap<>();

            m1.put("name", "低压故障");
            m1.put("value", isNullValue(stringObjectMap, "dygz"));
            m1.put("unit", "件");
            d1.add(m1);

            Map<String, Object> m2 = new HashMap<>();
            m2.put("name", "非电力故障");
            m2.put("value", isNullValue(stringObjectMap, "fdlgz"));
            m2.put("unit", "件");
            d1.add(m2);

            Map<String, Object> m3 = new HashMap<>();
            m3.put("name", "高压故障");
            m3.put("value", isNullValue(stringObjectMap, "gygz"));
            m3.put("unit", "件");
            d1.add(m3);
        }
        map.put("data", data);

        sql = "select dw , ifnull(bxhj , 0) bxhj ,ifnull(mswhbxl , 0) mswhbxl,ifnull(mswhbxlpjz ,0) mswhbxlpjz from zdgz_gzbx_bxgt  where dw!='大连' AND zbqs=(select max(zbqs) from zdgz_gzbx_bxgt) group by dw  order by dw ";
        result = queryInfoBySql(sql);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("label", "故障报修量统计（周）");
        map2.put("legend", new String[]{"报修合计（件）", "每十万户报修率（户）"});
        List<List<Map<String, Object>>> data2 = new ArrayList<>();
        List<Map<String, Object>> a1 = new ArrayList<>();
        data2.add(a1);
        List<Map<String, Object>> a2 = new ArrayList<>();
        data2.add(a2);
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("name", isNullValue(stringObjectMap, "dw"));
            m1.put("value", isNullValue(stringObjectMap, "bxhj"));
            a1.add(m1);
            Map<String, Object> m2 = new HashMap<>();
            m2.put("name", isNullValue(stringObjectMap, "dw"));
            m2.put("value", isNullValue(stringObjectMap, "mswhbxl"));
            a2.add(m2);

        }
        map2.put("data", data2);

        sql = "select dw , ifnull(qspjxfsc , 0) qspjxfsc,ifnull(bqxfsc , 0 )bqxfsc from zdgz_gzbx_gzqxpjclsc  where dw!='大连' AND zbqs=(select max(zbqs) from zdgz_gzbx_gzqxpjclsc) group by dw  order by dw ";
        result = queryInfoBySql(sql);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("label", "故障抢修修复时长（周）");
        map3.put("legend", new String[]{"本期修复时长（分）"});
        List<List<Map<String, Object>>> data3 = new ArrayList<>();
        List<Map<String, Object>> b1 = new ArrayList<>();
        data3.add(b1);
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("name", isNullValue(stringObjectMap, "dw"));
            m1.put("value", isNullValue(stringObjectMap, "bqxfsc"));
            b1.add(m1);
        }
        map3.put("data", data3);
        resultMap.put("left_top_2", new Object[]{map, map2, map3});
    }

    private void ywjxTwoRightTop2(Map<String, Object> resultMap) {
        String sql = "select * from ywjx_sykk  where zbqs = (select MAX(zbqs) from ywjx_sykk )";
        List<Map<String, Object>> result = queryInfoBySql(sql);

        Map<String, Object> m1 = new HashMap<>();
        m1.put("label", "自动化\n覆盖率");
        m1.put("value", isNullValue(result.get(0), "value1"));
        m1.put("unit", "%");
        Map<String, Object> m2 = new HashMap<>();
        m2.put("label", "线路条数");
        m2.put("value", isNullValue(result.get(0), "value2"));
        m2.put("unit", "条");
        Map<String, Object> m3 = new HashMap<>();
        m3.put("label", "自动化线路");
        m3.put("value", isNullValue(result.get(0), "value3"));
        m3.put("unit", "条");
        m1.put("data", new Object[]{m2, m3});

        Map<String, Object> m4 = new HashMap<>();
        m4.put("label", "馈线自动化\n覆盖率");
        m4.put("value", isNullValue(result.get(1), "value1"));
        m4.put("unit", "%");
        Map<String, Object> m5 = new HashMap<>();
        m5.put("label", "遥控次数");
        m5.put("value", isNullValue(result.get(1), "value2"));
        m5.put("unit", "次");
        Map<String, Object> m6 = new HashMap<>();
        m6.put("label", "遥控成功次数");
        m6.put("value", isNullValue(result.get(1), "value3"));
        m6.put("unit", "次");
        m4.put("data", new Object[]{m5, m6});

        Map<String, Object> m7 = new HashMap<>();
        m7.put("label", "终端\n在线率");
        m7.put("value", isNullValue(result.get(2), "value1"));
        m7.put("unit", "%");
        Map<String, Object> m8 = new HashMap<>();
        m8.put("label", "线路条数");
        m8.put("value", isNullValue(result.get(2), "value2"));
        m8.put("unit", "条");
        Map<String, Object> m9 = new HashMap<>();
        m9.put("label", "自动化线路");
        m9.put("value", isNullValue(result.get(2), "value3"));
        m9.put("unit", "条");
        m7.put("data", new Object[]{m8, m9});

        Map<String, Object> m10 = new HashMap<>();
        m10.put("label", "终端遥控\n使用率");
        m10.put("value", isNullValue(result.get(3), "value1"));
        m10.put("unit", "%");
        Map<String, Object> m11 = new HashMap<>();
        m11.put("label", "线路条数");
        m11.put("value", isNullValue(result.get(3), "value2"));
        m11.put("unit", "条");
        Map<String, Object> m12 = new HashMap<>();
        m12.put("label", "自动化线路");
        m12.put("value", isNullValue(result.get(3), "value3"));
        m12.put("unit", "条");
        m10.put("data", new Object[]{m11, m12});

        Map<String, Object> m13 = new HashMap<>();
        m13.put("label", "馈线自动化\n动作正确率");
        m13.put("value", isNullValue(result.get(4), "value1"));
        m13.put("unit", "%");
        Map<String, Object> m14 = new HashMap<>();
        m14.put("label", "线路条数");
        m14.put("value", isNullValue(result.get(4), "value2"));
        m14.put("unit", "条");
        Map<String, Object> m15 = new HashMap<>();
        m15.put("label", "自动化线路");
        m15.put("value", isNullValue(result.get(4), "value3"));
        m15.put("unit", "条");
        m13.put("data", new Object[]{m14, m15});

        resultMap.put("right_top_2", new Object[]{m1, m4, m7, m10, m13});
    }

    private void ywjxTwoRightTop1(Map<String, Object> resultMap) {
        String sql = "select '三遥可控开关'label,'台' unit ,value2 value from ywjx_sykk  where zbqs = (select MAX(zbqs) from ywjx_sykk ) and type = 5";
        resultMap.put("right_top_1", dldpResultService.queryInfoBySql2(sql));

    }

    private void ywjxThreeLeftTop1(Map<String, Object> resultMap) {
        String sql = "select sum(yczs) yczs,hb from ywjx_pbyc where zbqs=(select MAX(zbqs) from ywjx_pbyc) and pm='-'  group by hb";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("label", "配变异常（周）");
        map.put("value", isNullValue(result.get(0), "yczs"));
        map.put("unit", "台");
        map.put("label1", "环比");
        map.put("value1", isNullValue(result.get(0), "hb"));
        map.put("unit1", "%");
        resultMap.put("left_top_1", map);
    }

    private void ywjxThreeLeftTop2(Map<String, Object> resultMap) {
        //配变异常（周）
        String sql = "select yczs,hb,pbddyts,pbzgzts,pbsxbphts,dw,pm  from ywjx_pbyc where zbqs=(select MAX(zbqs) from ywjx_pbyc)";
        List<Map<String, Object>> result = queryInfoBySql(sql);

        List<Map<String, Object>> leftTop2 = new ArrayList<>();
        Map<String, Object> lxtj = new LinkedHashMap<>();
        List<Map<String, Object>> lxtjDataList = new ArrayList<>();
        Map<String, Object> lxtjddy = new LinkedHashMap<>();
        Map<String, Object> lxtjzgz = new LinkedHashMap<>();
        Map<String, Object> lxtjsxbph = new LinkedHashMap<>();

        Map<String, Object> yctj = new LinkedHashMap<>();
        List<List<Map<String, Object>>> yctjDataList = new ArrayList<>();
        List<Map<String, Object>> yctjDatazgzList = new ArrayList<>();
        List<Map<String, Object>> yctjDatasxbqhList = new ArrayList<>();
        List<Map<String, Object>> yctjDataddyList = new ArrayList<>();

        for (Map<String, Object> pbr : result) {
            if ("-".equals(pbr.get("pm"))) {
                lxtjddy.put("name", "低电压");
                lxtjddy.put("value", (pbr.get("pbddyts") != null ? String.format("%.2f", Double.parseDouble(pbr.get("pbddyts").toString()) / Double.parseDouble(pbr.get("yczs").toString()) * 100) : 0));
                lxtjddy.put("unit", "");
                lxtjzgz.put("name", "重过载");
                lxtjzgz.put("value", pbr.get("pbzgzts") != null ? String.format("%.2f", Double.parseDouble(pbr.get("pbzgzts").toString()) / Double.parseDouble(pbr.get("yczs").toString()) * 100) : 0);
                lxtjzgz.put("unit", "");
                lxtjsxbph.put("name", "三相不平衡");
                lxtjsxbph.put("value", pbr.get("pbsxbphts") != null ? String.format("%.2f", Double.parseDouble(pbr.get("pbsxbphts").toString()) / Double.parseDouble(pbr.get("yczs").toString()) * 100) : 0);
                lxtjsxbph.put("unit", "");
            } else {
                Map<String, Object> yctjZgz = new LinkedHashMap<>();
                yctjZgz.put("name", pbr.get("dw"));
                yctjZgz.put("value", pbr.get("pbzgzts") != null ? pbr.get("pbzgzts") : 0);
                yctjZgz.put("unit", "");
                yctjDatazgzList.add(yctjZgz);
                Map<String, Object> yctjSxbqh = new LinkedHashMap<>();
                yctjSxbqh.put("name", pbr.get("dw"));
                yctjSxbqh.put("value", pbr.get("pbsxbphts") != null ? pbr.get("pbsxbphts") : 0);
                yctjSxbqh.put("unit", "");
                yctjDatasxbqhList.add(yctjSxbqh);
                Map<String, Object> yctjDyy = new LinkedHashMap<>();
                yctjDyy.put("name", pbr.get("dw"));
                yctjDyy.put("value", pbr.get("pbddyts") != null ? pbr.get("pbddyts") : 0);
                yctjDyy.put("unit", "");
                yctjDataddyList.add(yctjDyy);
            }
        }
        yctjDataList.add(yctjDatazgzList);
        yctjDataList.add(yctjDatasxbqhList);
        yctjDataList.add(yctjDataddyList);

        lxtjDataList.add(lxtjzgz);
        lxtjDataList.add(lxtjsxbph);
        lxtjDataList.add(lxtjddy);
        lxtj.put("title", "配变异常类型统计（周）");
        lxtj.put("legend", new ArrayList<>());
        lxtj.put("data", new Object[]{lxtjDataList});

        yctj.put("title", "配变异常统计（周）");
        yctj.put("legend", new String[]{"重过载（项）", "三相不平衡（项）", "低电压（项）"});
        yctj.put("data", yctjDataList);

        leftTop2.add(lxtj);
        leftTop2.add(yctj);
        resultMap.put("left_top_2", leftTop2);
    }

    private void ywjxThreeRightTop1(Map<String, Object> resultMap) {
        String sql = "select xhtdshs,tb from ywjx_tdjbxx where zbqs=(select MAX(zbqs) from ywjx_tdjbxx) and pm = '-'";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("label", "大连消耗停电时户数（周）");
        map.put("value", isNullValue(result.get(0), "xhtdshs"));
        map.put("unit", "时户");
        map.put("label1", "同比");
        map.put("value1", isNullValue(result.get(0), "tb"));
        map.put("unit1", "%");
        resultMap.put("right_top_1", map);
    }

    private void ywjxThreeRightTop2(Map<String, Object> resultMap) {
        String sql = "select gdkkl from ywjx_gdjbxx where zbqs=(select max(zbqs) from ywjx_gdjbxx)";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new HashMap<>();
        map.put("label", "大连全口径供电可靠率（月）");
        map.put("value", isNullValue(result.get(0), "gdkkl"));
        map.put("unit", "%");
        resultMap.put("right_top_2", map);
    }

    private void ywjxThreeRightBottom1(Map<String, Object> resultMap) {
        String sql = "select dw,yaptd,gztd from ywjx_tdjbxx where dw!='大连' AND zbqs=(select max(zbqs) from ywjx_tdjbxx) group by dw order by dw ";
        List<Map<String, Object>> result = queryInfoBySql(sql);
        Map<String, Object> map = new HashMap<>();
        map.put("legend", new String[]{"预安排停电（时户）", "故障停电（时户）"});
        List<List<Map<String, Object>>> data = new ArrayList<>();
        List<Map<String, Object>> d1 = new ArrayList<>();
        data.add(d1);
        List<Map<String, Object>> d2 = new ArrayList<>();
        data.add(d2);
        List<Map<String, Object>> d3 = new ArrayList<>();
        data.add(d3);
        for (Map<String, Object> stringObjectMap : result) {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("name", isNullValue(stringObjectMap, "dw"));
            m1.put("value", new BigDecimal(isNullValue(stringObjectMap, "yaptd").toString()));
            d1.add(m1);
            Map<String, Object> m2 = new HashMap<>();
            m2.put("name", isNullValue(stringObjectMap, "dw"));
            m2.put("value", new BigDecimal(isNullValue(stringObjectMap, "gztd").toString()));
            d2.add(m2);
        }
        map.put("data", data);


        sql = "select gdkkl,nwgdkkl,cwgdkkl from ywjx_gdjbxx where zbqs=(select max(zbqs) from ywjx_gdjbxx)";
        result = queryInfoBySql(sql);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "农网供电可靠率");
        map2.put("value", isNullValue(result.get(0), "nwgdkkl"));
        map2.put("unit", "%");
        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "城网供电可靠率");
        map3.put("value", isNullValue(result.get(0), "cwgdkkl"));
        map3.put("unit", "%");
        Map<String, Object> map4 = new HashMap<>();
        map4.put("legend", new String[]{});
        map4.put("data", new Object[]{new Object[]{map2, map3}});
        resultMap.put("right_bottom_1", new Object[]{map, map4});
    }

    private List<Map<String, Object>> queryInfoBySql(String sql) {
        return dldpResultService.queryInfoBySql(sql);
    }

    private Object isNullValue(Map<String, Object> map, String key) {
        return map.get(key) != null ? map.get(key) : 0;
    }
}
